package com.ybkj.daijia.server.service.impl.vehicleInfo;

import com.ybkj.daijia.server.model.Company;
import com.ybkj.daijia.server.model.Driver;
import com.ybkj.daijia.server.service.CompanyService;
import com.ybkj.daijia.server.service.DriverService;
import com.ybkj.daijia.server.service.vehicleInfo.VehicleUsedStatusService;
import com.ybkj.daijia.server.vehicleInfo.Vehicle;
import com.ybkj.daijia.server.mapper.VehicleMapper;
import com.ybkj.daijia.server.service.vehicleInfo.VehicleService;
import com.ybkj.daijia.server.vehicleInfo.VehicleUsedStatus;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/***
 * @author: huguigang
 * @date: 2019-12-11
 * @description:
 */
@Service
public class VehicleServiceImpl implements VehicleService {

    @Autowired
    private VehicleMapper vehicleMapper;

    @Autowired
    private CompanyService companyService;

    @Autowired
    private DriverService driverService;

    @Autowired
    private VehicleUsedStatusService vehicleUsedStatusService;

    @Override
    public int createVehicle(Vehicle vehicle) {
        int result = vehicleMapper.createVehicle(vehicle);
        return result;
    }

    @Override
    public List<Vehicle> getCarByCarNumber(String vehicleNumber) {
        List<Vehicle> vehicle = vehicleMapper.getCarByCarNumber(vehicleNumber);
        return vehicle;
    }

    @Override
    public void deleteCar(String id){
        int result = vehicleMapper.deleteVehicle(id);
    }

    @Override
    public Vehicle getVehicleById(String id) {
        return vehicleMapper.getVehicleById(id);
    }

    @Override
    public int editVehicle(Vehicle vehicle) {
        int result = vehicleMapper.editVehicle(vehicle);
        return result;
    }

    @Override
    public List<Vehicle> getVehicleList(Long companyId, String vehicleNumber) {
        List<Vehicle> vehicleList = vehicleMapper.getVehicleList(companyId, vehicleNumber);
        return vehicleList;
    }

    @Override
    public List<Vehicle> getVehiclesByParams(String vehicleNumber, String companyId, String driverName, String driverPhone, String userName) {
        List<Company> companyList = companyService.getCompanysById(companyId);
        List<Long> companyIdList = companyList.size() > 0 ? new ArrayList<>() : null;
        for(Company company : companyList){
            companyIdList.add(company.getId());
        }
        //根据驾驶员姓名和电话查询驾驶员
        List<Driver> driverList = driverService.queryDriverByParams(driverPhone, driverName);
        List<Long> driverIdList = null;
        if(null != driverList && driverList.size() > 0) {
            driverIdList = new ArrayList<>();
            for(Driver dri : driverList) {
                driverIdList.add(dri.getId());
            }
        }
        //查询驾驶员绑定的车辆
        List<VehicleUsedStatus> vusList = vehicleUsedStatusService.queryUsedInfo(driverIdList);
        //保存车辆id
        List<String> vehicleIdList = null;
        if(vusList.size() > 0) {
            vehicleIdList = new ArrayList<>();
            for(VehicleUsedStatus vus : vusList) {
                vehicleIdList.add(vus.getCarId());
            }
        }
        List<Vehicle> vehicleList = vehicleMapper.getVehiclesByParams(vehicleNumber, companyIdList, vehicleIdList);
        return vehicleList;
    }
}
